package com.txzt.gaea.common.util;

import com.alibaba.fastjson.JSONObject;
import com.txzt.gaea.common.constant.Constants;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by P318 on 2019/6/20.
 */
public class HttpRequestUtil {

    /**
     * get请求
     * @param map
     * @param url
     * @return
     */
    public static JSONObject doGet(Map<String,String> map, String url){
        String result = "";
        StringBuilder urlparas = new StringBuilder();
        HttpClient client = new HttpClient();
        Iterator<Map.Entry<String,String>> it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String,String> entry = it.next();
            urlparas.append(entry.getKey()).append("=").append(entry.getValue());
            if(it.hasNext()){
                urlparas.append("&");
            }
        }
        GetMethod getMethod = new GetMethod(url+"?"+urlparas);
        getMethod.setRequestHeader("toolCode", Constants.DOCUMENT_TOOL_CODE);
        getMethod.setRequestHeader("authCode", Constants.OSS_AUTH_CODE);
        int code = 0;
        try {
            code = client.executeMethod(getMethod);
            if(200 == code){
                result = getMethod.getResponseBodyAsString();
            }
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(result);
    }
}
